/******************************************************************************************************************
* File:SmokeSensor.java
* Course: 17655
* Project: Assignment A3
*
*
* Description:
*
* This class simulates a Security Break Sensor.
 *
* Parameters: IP address of the event manager (on command line). If blank, it is assumed that the event manager is
* on the local machine.
*
* Internal Methods:
*
*
*/
import EventPackage.*;
import TermioPackage.*;

public class SmokeSensor {
    public static void main(String args[])
    {
        String EvtMgrIP;     // Event Manager's IP address
        char UserEntry;
        int EventID = 12;              //Event ID
        int HBDeviceID = 116;              //Heartbeat DeviceID
        EventManagerInterface em = null; // Interface object to event manager
        Termio UserInput = new Termio();
        boolean Done = false;
        HeartBeat heartBeat = null;

        if ( args.length == 0 )
        {
            heartBeat = new HeartBeat(HBDeviceID);


            System.out.println("\n\n attempting to register on local machine");
            try
            {
                em  = new EventManagerInterface();
            }catch(Exception e)
            {
                System.out.println("Error instantiating event manager interface:" + e);

            }//catch

        }else{
            // event manager is not on local system
            EvtMgrIP = args[0];
            heartBeat = new HeartBeat(HBDeviceID,EvtMgrIP);

            System.out.println("\n\nAttempting to register on the machine:: " + EvtMgrIP );
            try
            {
                em = new EventManagerInterface( EvtMgrIP );
            }catch(Exception e)
            {
                System.out.println("Error instantiating event manager interface:" + e);
            }
        }
            // check to see if registration worked
        if(em != null )
        {
             // Begin simulation
            heartBeat.start();
            System.out.println("Beginning Simulation...");
            while(!Done)
            {
                 System.out.println("Press the <A> key to simulate a 40 second smoke event (20s with smoke, remainder without smoke)");
                 System.out.println("Press the <B> key to simulate a 30 second smoke event (10s with smoke, remainder without smoke)");
                 System.out.println("Press the <X> key to exit the simulation");

                 UserEntry = UserInput.KeyboardReadChar();
                 if((UserEntry == 'a')||(UserEntry == 'A'))
                 {
                     
                     try
                     {
                         System.out.println("Sending 40s sensor detection event (20s with smoke, remainder without smoke)");
                         long t0 = System.currentTimeMillis();
                         while (System.currentTimeMillis() - t0 < 40000) {
                            Event event;
                             if (System.currentTimeMillis() - t0 < 20000) {
                                event = new Event(EventID, "1");
                             } else {
                                event = new Event(EventID, "0");
                            }
                            em.SendEvent(event);
                            try {
                                Thread.sleep(1000); //sensor has 1000 ms fidelity
                            } catch (InterruptedException e) {
                            }
                         }
                     }catch(Exception ex)
                     {
                        System.out.println("Error sending event" + ex);
                     }
                 } else if((UserEntry == 'b')||(UserEntry == 'B'))
                 {
                     try
                     {
                         System.out.println("Sending 30s sensor detection event (10s with smoke, remainder without smoke)");
                         long t0 = System.currentTimeMillis();
                         while (System.currentTimeMillis() - t0 < 30000) {
                            Event event;
                             if (System.currentTimeMillis() - t0 < 10000) {
                                event = new Event(EventID, "1");
                             } else {
                                event = new Event(EventID, "0");
                            }
                            em.SendEvent(event);
                            try {
                                Thread.sleep(1000); //sensor has 1000 ms fidelity
                            } catch (InterruptedException e) {
                            }
                         }
                     }catch(Exception ex)
                     {
                        System.out.println("Error sending event" + ex);
                     }

                 }else if ((UserEntry == 'x' ) || (UserEntry == 'X'))
                 {
                     Done = true;
                     try{
                        em.UnRegister();
                     }catch(Exception ex)
                     {
                         System.out.println("Unable to unregister from event manager" + ex);
                     }
                 }//if

            }//while

        }
        else
        {
            System.out.println("Unable to register with event manager ");
        }//if
        heartBeat.Close();
        //main

    }

}
